package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.Orders;
import com.wzcl.app.model.dto.IosOrdersDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

public interface OrdersRepository extends JpaRepository<Orders,Integer>, JpaSpecificationExecutor<Orders> {
    @Query("SELECT m FROM Orders m WHERE m.no = ?1 AND m.state = 0")
    Orders findByOrderNo(String no);

    @Modifying
    @Transient
    @Query("UPDATE Orders m SET m.payState = ?1 WHERE m.no = ?2")
    void setPayStateByNo(Integer payState, String no);

    /**
     * 根据订单号修改订单状态
     * @param payState
     * @param orderState
     * @param no
     */
    @Modifying
    @Transactional
    @Query("UPDATE Orders m SET m.payState = ?1,m.ordersState = ?2 WHERE m.no = ?3")
    void setPayStateAndOrdersStateByNo(Integer payState, Integer orderState, String no);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("UPDATE Orders m SET m.payState = ?1,m.ordersState = ?2,m.sysType = ?3 WHERE m.no = ?4")
    void setPayStateAndOrdersStateByNo(Integer payState, Integer orderState, Integer sysType, String no);

    Orders findByUserPhoneAndCreateFrom(String phone, String createFrom);

    @Query("SELECT  m FROM Orders m WHERE m.userPhone = ?1 AND m.createFrom LIKE ?2 ")
    List<Orders> findAllByUserPhoneAndCreateFromLike(String phone, String createFrom);

    @Query("SELECT  m FROM Orders m WHERE m.createFrom LIKE ?1 AND m.payState = 5")
    List<Orders> findAllByCreateFromLike(String createFrom);

    // 查询当前用户已创建未支付的有效订单（未删除）
    @Query("SELECT  m FROM Orders m WHERE m.payState = 0 AND m.ordersState = 0 AND m.state = 0 AND m.userPhone = ?1")
    List<Orders> findAllNotPayByPayState(String userPhone);

    // 查询当前用户最近一条未支付的有效订单（未删除）
    @Query("SELECT  m FROM Orders m WHERE m.payState = 0 AND m.ordersState = 0 AND m.state = 0 AND m.userPhone = ?1 and m.id = (select max(m.id) from Orders m)")
    Orders findAllNotPayByPayStateOfRecently(String userPhone);

    //查询所有用户未支付的有效订单
    @Query("SELECT  m FROM Orders m WHERE m.payState = 0 AND m.ordersState = 0 AND m.state = 0")
    List<Orders> findAllPayState();

    //根据手机号修改用户订单状态
    @Query("UPDATE Orders m SET m.ordersState = 1 WHERE m.userPhone = ?1")
    @Modifying
    @Transactional
    void updateOrderStateByPhone(String userPhone);

    // 根据订单号查询订单
    @Query(value = "select m from Orders m where m.no = ?1")
    Orders findAllByOrderNumber(String no);

    // 更改系统类型
    @Query(value = "update Orders o set o.sysType = ?1 where o.no = ?2")
    @Modifying
    @Transactional
    void updateSysType(Integer sysType, String orderNumber);

    //根据订单信息修改订单状态
    @Query(value = "update Orders m SET m.payState = ?1 WHERE m.no = ?2")
    @Modifying
    @Transactional
    void updateStateByOrderNumber(Integer payState, String orderNumber);
}
